/**
 * cn.ucox.web.application.config.WebInit
 *
 * @author chenw
 * @create 16/2/3.12:23
 * @email javacspring@hotmail.com
 */

package cn.com.oceansoft.application.config;

import cn.com.oceansoft.application.filter.UnknownErrorFilter;
import cn.com.oceansoft.application.listener.SystemInitializer;
import cn.com.oceansoft.framework.servlet.UcoxDispatcherServlet;
import cn.com.oceansoft.framework.servlet.WechatServlet;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.HiddenHttpMethodFilter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * Web应用启动初始化
 *
 * @author chenw
 * @time 16/2/3 12:23
 * @email javacspring@gmail.com
 */
//public class WebInit implements WebApplicationInitializer {

//    @Override
//    public void onStartup(ServletContext servletContext) throws ServletException {
        //注册日志
//        servletContext.setInitParameter("log4jConfigLocation", "classpath:properties/log4j.properties");
//        servletContext.addListener("org.springframework.web.util.Log4jConfigListener");
//
//        //加载系统核心配置(数据源/持久层/业务层/扩展自定义组件)
//        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
//        rootContext.register(BeansKit.class);
//        rootContext.register(AppConfig.class);
//
//        rootContext.register(MyBatisConfig.class);
//        rootContext.register(DataSourceConfig.class);
//        rootContext.register(RedisConfig.class);
//        rootContext.register(JPAConfig.class);
//        rootContext.register(ServiceConfig.class);
//        rootContext.refresh();
//        //注册自定义系统初始化组件
//        servletContext.addListener(SystemInitializer.class);
//        //注册过滤器
//        servletContext.addFilter("hiddenHttpMethodFilter", new HiddenHttpMethodFilter());
//        servletContext.addFilter("pageNotFoundErrorFilter", new UnknownErrorFilter());
//
//        //SpringMVC配置信息
//        AnnotationConfigWebApplicationContext mvcDispatcher = new AnnotationConfigWebApplicationContext();
//        mvcDispatcher.register(ControllerConfig.class);
//        mvcDispatcher.setServletContext(servletContext);
//
//        //增加Dispatcher映射
//        servletContext.addListener(new ContextLoaderListener(rootContext));
//
//        //注册自定义Servlet
//        ServletRegistration.Dynamic wechatServlet = servletContext.addServlet("wechat", new WechatServlet());
//        wechatServlet.addMapping("/wechat/");
//        wechatServlet.setLoadOnStartup(2);
//        wechatServlet.setAsyncSupported(true);

        //核心DispatcherServlet配置
//        DispatcherServlet servlet = new DispatcherServlet(mvcDispatcher);
//        servlet.setThrowExceptionIfNoHandlerFound(true);
//        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", servlet);
//        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(mvcDispatcher));

//        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new UcoxDispatcherServlet(mvcDispatcher));
//        dispatcher.setLoadOnStartup(1);
//        dispatcher.addMapping("/");

//        mvcDispatcher.refresh();
//        mvcDispatcher.close();
//    }
//}
